package cn.seapon.talkerserver.common;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 网络传送消息的载体
 * 需要装换成json对象才能进行网络传输
 * @author Asuron
 * Created on 2015年1月3日 下午9:59:06
 */
public class TMessage{
	private static final String TAG = "__TMessage类__";

	//发送者ID
	private String senderId = "";
	//发送时间
	private long sendTime = 0;
	//消息类型
	private 	int genre = 0;
	//消息内容
	private MessageContent content = new MessageContent();
	
	
	public JSONObject getJsonObject(){
		JSONObject jsonObject = new JSONObject();
		try {
			JSONObject messageContent = content.getJsonObject();
			jsonObject.put("senderId", senderId);
			jsonObject.put("sendTime", sendTime);
			jsonObject.put("genre", genre);
			jsonObject.put("content", messageContent);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return jsonObject;
	}
	public static TMessage getObjectFromJson(JSONObject jsonObject) {
		TMessage tMessage = new TMessage();
		try {
			String senderId = jsonObject.getString("senderId");
			long sendTime = jsonObject.getLong("sendTime");
			int genre = jsonObject.getInt("genre");
			MessageContent content = MessageContent.getObjectFromJson(jsonObject.getJSONObject("content"));
			
			tMessage.setContent(content);
			tMessage.setGenre(genre);
			tMessage.setSenderId(senderId);
			tMessage.setSendTime(sendTime);
			
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return tMessage;
	}
	
	public String getJSonString(){
		JSONObject jsonObject = getJsonObject();
		return jsonObject.toString();
	}
	

	
	public TMessage() {
	}
	public String getSenderId() {
		return senderId;
	}
	public long getSendTime() {
		return sendTime;
	}
	public int getGenre() {
		return genre;
	}
	public MessageContent getContent() {
		return content;
	}
	
	public void setSenderId(String senderId) {
		this.senderId = senderId;
	}

	public void setSendTime(long sendTime) {
		this.sendTime = sendTime;
	}

	public void setGenre(int genre) {
		this.genre = genre;
	}

	public void setContent(MessageContent content) {
		this.content = content;
	}
	
	
	
}
